Skip to content

Rework install_unit() and remove_unit() - #449

Merged
Enchufa2 merged 7 commits into
mainfrom
rework-user-units
Nov 13, 2026
Merged

Enchufa2 merged 7 commits into
mainfrom
rework-user-units

Conversation

@Enchufa2

Copy link
Copy Markdown
Member

Fixes #448. Supersedes #447.

Problem

install_unit() maps each symbol/name with several separate udunits calls (id → unit and unit → id, in more than one encoding), so a refusal halfway leaves some of them behind. remove_unit() then unmaps the symbol and name of whatever unit an id points to, so removing a leftover, or any alias, strips an existing unit:

install_unit("zzdeg", "1 degree")   # refused, but zzdeg stays mapped
remove_unit("zzdeg")
set_units(1, "degree")              # degree has lost its symbol

Reordering the calls doesn't help, because udunits keeps a unit's own symbol in per-encoding trees with lookup fallbacks: degree's symbol is in the UTF-8 tree, m's is in the ASCII tree, so every fixed order corrupts some unit (e.g., UTF-8 first turns 1 [m] into 1 [zzm]).

Changes

  • Atomic install. Every mapping is planned first; an id that belongs to another unit is refused before anything is mapped. udunits errors are read from return codes (handler silenced), and applied mappings are rolled back on any failure.
  • Aliases. If the unit already has a symbol (resp. name), new ones are installed as aliases, e.g. install_unit("deg2", "1 degree") now works and degree keeps ° for output.
  • Encodings. id → unit is mapped once (udunits ignores the encoding there). unit → id goes to the ASCII tree for ASCII ids, which every encoding falls back to, so Extend convert_to_base() to user-defined base units? #437 keeps working.
  • Registry. Each install records the mappings it made; remove_unit() undoes exactly those, hands the unit's own symbol/name to a remaining alias when it is removed, and leaves ids it didn't install untouched with a warning. The registry is cleared with the unit system in load_units_xml().
  • Ids are trimmed on a copy instead of in place in R's string cache; NA and empty ids are rejected.
  • The code moves to src/user_units.cpp (separate commit, pure move).

Behavior change: remove_unit() on a symbol/name from the database (e.g. remove_unit("m")) used to remove it; now it warns and does nothing. Removing an unknown id is still silent.

Testing

  • New tests in test_user_conversion.R: refused installs map nothing, aliases, alias removal and promotion, removal of database units, ASCII availability. They fail on main (7 errors/failures) and pass here; the full suite passes.
  • A sweep that installs and removes a symbol and a name for every unit in the database leaves all of them unchanged under UTF-8, Latin-1 and ASCII (main: 3 units corrupted under UTF-8).

🤖 Generated with Claude Code

@codecov

codecov Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.22%. Comparing base (ecbaa91) to head (ae73aa7).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #449      +/-   ##
==========================================
+ Coverage   92.95%   94.22%   +1.26%     
==========================================
  Files          21       22       +1     
  Lines        1207     1315     +108     
==========================================
+ Hits         1122     1239     +117     
+ Misses         85       76       -9     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Enchufa2 added a commit that referenced this pull request Nov 13, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Enchufa2 and others added 7 commits November 13, 2026 13:10
A refused install_unit() could leave symbols or names half mapped, and
remove_unit() unmapped the symbol and name of whatever unit an id pointed
to, so removing a leftover or an alias stripped an existing unit (#448).

- install_unit() plans every mapping first and refuses before mapping
  anything when an id belongs to another unit; udunits errors are read
  from return codes and any applied mapping is rolled back on failure.
- Symbols and names for a unit that already has one become aliases
  (e.g., install_unit("deg2", "1 degree")) instead of being refused.
- id -> unit is mapped once (udunits ignores the encoding there), and
  unit -> id goes to the ASCII tree for ASCII ids, which every encoding
  falls back to (keeps #437 working).
- A registry records the mappings each install made; remove_unit() undoes
  exactly those, promotes a remaining alias when a unit's own symbol or
  name is removed, and warns and leaves alone ids it did not install.
- Ids are trimmed on a copy instead of in R's string cache; NA and empty
  ids are rejected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pure move of the install/remove code out of udunits.cpp. The shared
state (sys, enc) and registry_clear() are declared in a new udunits.h
inside a `units` namespace, so they are not exported as bare globals.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
udunits' own UTF-8 check lets some invalid strings through (e.g. "\xff"),
which were then stored as a unit's symbol. Check with validEnc() in
install_unit() and remove_unit(), sharing the argument checks in
.check_ids(). With conflicts refused when planning and invalid strings
refused here, the rollback in ud_install() is only reachable on a system
error, so mark it nocov.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Test argument checks, NA and empty ids, promotion of the oldest alias
when there are several, and non-ASCII own symbols. Mark as nocov the two
guards that cannot be reached: undoing a unit's own id that has changed
since, and ud_install() without a unit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Enchufa2
Enchufa2 merged commit 3652916 into main Nov 13, 2026
30 checks passed
@Enchufa2
Enchufa2 deleted the rework-user-units branch November 13, 2026 12:35
Enchufa2 added a commit that referenced this pull request Nov 18, 2026
…database ids (#452)

* Refuse ids that udunits already reads as a different unit

install_unit() only checked the map of the same kind, but udunits reads an
identifier by name (whatever its case), then by symbol, then the same after
stripping a prefix. So a new name "m" or symbol "km" silently redefined a
database unit, and a symbol "gal" was accepted but never reached, since the
database name "gal" wins over it.

- Refuse an id that udunits currently reads as a different unit.
- Refuse a name with a case variant that is read as a different unit (e.g.
  "KG" would shadow "kg"). Names with at most 12 letters try every variant;
  no database symbol, prefixed or not, is longer than 8 characters, so
  longer names are checked against installed symbols only.
- After mapping, every id must be read back as the new unit (e.g. "zz1" is
  read as zz^1); otherwise roll back and refuse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Warn in remove_unit() about any id udunits reads as a unit

remove_unit() only warned about ids it had not installed when they were
in the map of their own kind, so removing "km" (read by prefix), a name
"m" (a database symbol) or a symbol "gal" (a database name) did nothing
silently. Decide by what udunits reads the id as instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Add override argument to install_unit() to redefine database ids

Before #449, remove_unit() on a database id followed by install_unit() was
the only way to give such an id another meaning, and it is no longer
possible (#451). install_unit(..., override = TRUE) does it in one step
and reversibly:

- The database mappings that give an id another meaning are set aside:
  the id in both the symbol and name maps, and the unit's own symbol or
  name when it is that id. They are recorded under the installed id, and
  remove_unit() puts them back after undoing the install.
- Setting aside, mapping and the read-back check run as one transaction,
  rolled back on any failure.
- What each id meant before is reported with a message, by whatever route
  (e.g. "km" by prefix).
- Ids installed by install_unit() must be removed first, and other ids a
  new name would shadow are still refused; case variants of the name
  itself (e.g. "microN" for "micron") are not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* NEWS: merge the install_unit() entries, reference #452 and #451

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Share what an override set aside between both kinds of the same id

A database mapping set aside by an override was filed under whichever
installed id set it aside first. When an id was overridden as both a
symbol and a name, removing the symbol first tried to put the database
name back while the new name still held it; the failure went unnoticed,
and the database name was lost once the new name was removed (#451).

Any installed id, of either kind, that would set a mapping aside needs it
out of the way: symbols as they are, names whatever their case. So when
an id is removed, a mapping it set aside is handed to such an id if one
is left, and put back only when none is. This works in any removal order,
for ids installed in one call or in separate ones, and keeps the other
kind's override working when only one kind is removed.

- A mapping that cannot be put back is reported instead of ignored.
- remove_unit() no longer warns "left unchanged" about an id that the
  same call put back, e.g. remove_unit("gal", "gal") after overriding
  only the symbol.
- The override message and the docs say that overriding a symbol sets
  aside the name spelled the same in any letter case.

Supersedes #453, whose test is included.

Co-Authored-By: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Robin Shannon <32329438+vorpalvorpal@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A refused install_unit() leaves the symbol mapped, and remove_unit() then strips the existing unit's symbol

1 participant